package com.wxx.util;

import com.wxx.model.Task;
import org.apache.commons.lang3.StringUtils;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzUtils {

    /**
     * 添加新任务
     * @param task
     * @return
     * @throws SchedulerException
     * @throws ClassNotFoundException
     */
    public static boolean addJob(Task task) throws SchedulerException, ClassNotFoundException {
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        Class clazz = Class.forName(task.getJobClass());

        JobDetail jobDetail = JobBuilder.newJob(clazz)
                .withIdentity(task.getJobName(), task.getJobGroup())
//                .usingJobData()
                .build();

        CronTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(task.getJobName(), task.getJobGroup())
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule(task.getCronExpression()))
                .build();
        scheduler.scheduleJob(jobDetail,trigger);
        scheduler.start();
        return true;
    }

    /**
     * 删除一个定时任务
     * @param jobName       任务名称
     * @param jobGroup      任务组别
     * @return
     * @throws SchedulerException
     */
    public static boolean deleteJob(String jobName, String jobGroup) throws SchedulerException {
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
        scheduler.pauseTrigger(triggerKey);
        scheduler.unscheduleJob(triggerKey);
        scheduler.deleteJob(JobKey.jobKey(jobName, jobGroup));
        return true;
    }

    /**
     * 更新定时任务
     * @param jobName
     * @param jobGroup
     * @param cronExpression
     * @return
     * @throws SchedulerException
     */
    public static boolean updateJob(String jobName, String jobGroup,String cronExpression)  {
        try {
            SchedulerFactory factory = new StdSchedulerFactory();
            Scheduler scheduler = factory.getScheduler();
            TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
            ScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            //按照新的cron表达式重新构建trigger
            trigger = (CronTrigger) trigger.getTriggerBuilder()
                    .withIdentity(triggerKey)
                    .withSchedule(scheduleBuilder)
                    .build();
            //按照新的重新执行
            scheduler.rescheduleJob(triggerKey,trigger);
            return true;
        } catch (SchedulerException e) {
            e.printStackTrace();
            System.err.println("任务更新失败{}" + e.toString() );
            return false;
        }
    }

    /**
     * 停止job
     *
     * @param name  名称
     * @param group 分组
     */
    public static Boolean stopJob(String name, String group) throws SchedulerException {
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        JobKey jobKey = JobKey.jobKey(name, group);
        try {
            scheduler.pauseJob(jobKey);
            return true;
        } catch (SchedulerException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 恢复指定任务
     * @param name
     * @param group
     */
    public static Boolean restartJob(String name, String group) throws SchedulerException {
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        JobKey jobKey = JobKey.jobKey(name, group);
        try {
            scheduler.resumeJob(jobKey);
            return true;
        } catch (SchedulerException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 停止所有任务
     */
    public static Boolean stopAll() throws SchedulerException {
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        try {
            scheduler.pauseAll();
            return true;
        } catch (SchedulerException e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 停止所有任务
     */
    public static Boolean restartAll() throws SchedulerException {
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        try {
            scheduler.resumeAll();
            return true;
        } catch (SchedulerException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 检查job是否存在
     * @param jobName
     * @param jobGroup
     * @return
     * @throws SchedulerException
     */
    public static boolean isResume(String jobName, String jobGroup) throws SchedulerException {
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
        boolean b = scheduler.checkExists(triggerKey);
        return b;
    }


}
